﻿using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.Repository;
using Ocelot.DependencyInjection;
using SX.GateWay.Override;

namespace SX.GateWay
{
    public static class OcelotBuilderExtensions
    {
        /// <summary>
        /// Ocelot配置保存到数据库
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="apiGatewayKey"></param>
        /// <returns></returns>
        public static IOcelotBuilder AddConfigStoredInSqlServer(this IOcelotBuilder builder, string apiGatewayKey)
        {
            builder.Services.AddHostedService<FileConfigurationPoller>();
            builder.Services.AddSingleton<IFileConfigurationRepository>(sp =>
            {
                return new SqlServerConfigurationRepository(sp, apiGatewayKey);
            });
            return builder;
        }
    }
}
